package com.ewanghuiju.app.ui.login.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.login.AddInvitecodeContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.response.InviterInfo;
import com.ewanghuiju.app.model.bean.response.LoginResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.http.request.BaseRequest;
import com.ewanghuiju.app.model.prefs.ImplPreferencesHelper;
import com.ewanghuiju.app.presenter.login.AddInvitecodePresenter;
import com.ewanghuiju.app.ui.main.activity.MainActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.ewanghuiju.app.widget.popupwindow.CustomTipPopup;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.muddzdev.styleabletoast.StyleableToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 添加邀请码界面
 * @time 2020/2/14
 */
public class AddInvitecodeActivity extends BaseActivity<AddInvitecodePresenter> implements AddInvitecodeContract.View {
    @BindView(R.id.et_invitecode)
    EditTextWithDel mEtinvitecode;
    @BindView(R.id.login_text)
    TextView loginText;
    @BindView(R.id.tv_yqcode_lab)
    TextView tvYqcodeLab;
    @BindView(R.id.iv_invitehead)
    RoundedImageView ivInvitehead;
    @BindView(R.id.tv_invite_name)
    TextView tvInviteName;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.layout_invite_info)
    LinearLayout layoutInviteInfo;

    private String phoneNumber, unionid, mNickname, mHeadimgurl;
    private int mType;

    private CustomTipPopup mCustomTipPopup;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_add_invitecode;
    }

    @Override
    protected void initEventAndData() {

        mType = getIntent().getIntExtra(Constants.BIND_USERINFO, 1);
        phoneNumber = getIntent().getStringExtra(Constants.REGIST_PHONE);
        if (mType == 2) {
            unionid = getIntent().getStringExtra(Constants.BIND_UNIONID);
            mNickname = getIntent().getStringExtra(Constants.BIND_NICKNAME);
            mHeadimgurl = getIntent().getStringExtra(Constants.BIND_HEADIMGURL);
        }
        mEtinvitecode.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 3) {
                    BaseRequest baseRequest = new BaseRequest();
                    baseRequest.setNumber(s.toString());
                    mPresenter.getInviterinfo(baseRequest);
                } else {
                    layoutInviteInfo.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.tv_next, R.id.iv_yiwen})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_next:
                String code = mEtinvitecode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    StyleableToast.makeText(mContext, "请输入邀请码", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                LoadingDialogUtils.show(mContext);
                if (mType == 2) {
                    mPresenter.wxUserRegister(unionid, phoneNumber, code, mHeadimgurl, mNickname);
                } else {
                    mPresenter.bindUserMobileparentid(phoneNumber, code);
                }
                break;
            case R.id.iv_yiwen:
                //提示
                if (mCustomTipPopup != null) {
                    mCustomTipPopup.showPopupWindow();
                    return;
                }
                mCustomTipPopup = new CustomTipPopup(mContext, getString(R.string.popup_tip_code_title),
                        getString(R.string.popup_tip_code_content), null);
                mCustomTipPopup.setPopupGravity(Gravity.CENTER);
                mCustomTipPopup.showPopupWindow();

                break;
            default:
                break;
        }
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        Glide.clear(mImghead);
    }

    @Override
    public void showInviterinfo(InviterInfo inviterInfo) {
        layoutInviteInfo.setVisibility(View.VISIBLE);
        tvInviteName.setText(inviterInfo.getNickname());
        tvInviteCode.setText(inviterInfo.getMobile() + "");
        ImageLoader.load(mContext, inviterInfo.getAvatar(), ivInvitehead);
    }

    @Override
    public void showInviterinfoErrorMsg(int code, String message) {
//        StyleableToast.makeText(mContext, message, Toast.LENGTH_SHORT, R.style.mytoast).show();
        layoutInviteInfo.setVisibility(View.INVISIBLE);
    }

    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
//        App.getInstance().finishActivity();
        new StartActivityUtil(mContext, MainActivity.class)
                .startActivity(false);
        onBackPressedSupport();
        slideRightIn();
    }

    @Override
    public void showBindUserMobileInfo(LoginResponBean loginResponBean) {
        //绑定邀请人成功
        LoadingDialogUtils.dismissDialog_ios();
        if (!TextUtils.isEmpty(loginResponBean.getToken())) {
            ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
            mImplPreferencesHelper.setToken(loginResponBean.getToken());
            //获取用户信息  到达首页
            LoadingDialogUtils.show(mContext);
            mPresenter.getUserInfo();
        }

    }

    @Override
    public void showWxUserRegister(LoginResponBean bean) {
        //微信注册成功回调
        LoadingDialogUtils.dismissDialog_ios();
        if (bean != null && !TextUtils.isEmpty(bean.getToken())) {
            ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
            mImplPreferencesHelper.setToken(bean.getToken());
            //获取用户信息  到达首页
            LoadingDialogUtils.show(mContext);
            mPresenter.getUserInfo();
        }
    }
}
